package com.code;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.Global.Global;
import com.example.R;
import com.object.obNuocVo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class TK_NuocVo extends Activity {

	TextView tvTitleC2, tvTitleC3;
	private int iChooseNuocVo;
	private String strDate;
	private String strKhos;
	private ArrayList<String> colNames;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tk_nuocvo);

		tvTitleC2 = (TextView) findViewById(R.id.tvTitleC2);
		tvTitleC3 = (TextView) findViewById(R.id.tvTitleC3);
		ListView lvnuocVo = (ListView) findViewById(R.id.lvNuocVo);

		iChooseNuocVo = super.getIntent().getExtras().getInt("iChooseNuocVo");
		strDate = super.getIntent().getExtras().getString("strDate");
		strKhos = super.getIntent().getExtras().getString("strKhos");
		colNames = super.getIntent().getExtras().getStringArrayList("colNames");

		if (iChooseNuocVo == TK_ChonMau.CHOOSE_XNUOC_NVO) {
			tvTitleC2.setText("Xuất nước");
			tvTitleC3.setText("Nhập vỏ");
		} else if (iChooseNuocVo == TK_ChonMau.CHOOSE_XVO_NNUOC) {
			tvTitleC2.setText("Xuất vỏ");
			tvTitleC3.setText("Nhập nước");
		} else if (iChooseNuocVo == TK_ChonMau.CHOOSE_TONNV) {
			tvTitleC2.setText("Tồn nước");
			tvTitleC3.setText("Tồn vỏ");
		}

		ArrayList<obNuocVo> nuocvos = getNuocVos(strDate, strDate, strKhos);
		NuocVoAdapter dataAdapter = new NuocVoAdapter(this, R.layout.tk_nuocvo,
				nuocvos);
		lvnuocVo.setAdapter(dataAdapter);

	}

	public ArrayList<obNuocVo> getNuocVos(String strNgayDB, String strNgayKT,
			String strKho) {
		String strCol1 = colNames.get(0).toString();
		String strCol2 = colNames.get(1).toString();
		String strCol3 = colNames.get(2).toString();
		Double dbTotalCol2 = (double) 0;
		Double dbTotalCol3 = (double) 0;
		ArrayList<obNuocVo> nuocvos = new ArrayList<obNuocVo>();
		String sql = "spMobileDKNhatXuatNuocALLVoRong '" + strNgayDB + "', '"
				+ strNgayKT + "', '" + strKho + "'";
		ResultSet rs = Global.excuteQuerySQLServer(sql);
		try {
			while (rs.next()) {
				obNuocVo obnv = new obNuocVo();
				// check value for 2 column ,if 2 column is "" then continue
				if (rs.getString(strCol2) != null
						&& (rs.getString(strCol2).toString().equals("0") || rs
								.getString(strCol2).toString().equals("0.0"))
						&& (rs.getString(strCol3) != null && (rs
								.getString(strCol3).toString().equals("0") || rs
								.getString(strCol3).toString().equals("0.0"))))
					continue;//edit by trangbm date 04/04/2015
				if (rs.getString(strCol1) != null) {
					obnv.setMaSP(rs.getString(strCol1).toString());
				}
				if (rs.getString(strCol2) != null) {
					if (rs.getString(strCol2).toString().equals("0")
							|| rs.getString(strCol2).toString().equals("0.0"))
						obnv.setContentC2("");
					else
						obnv.setContentC2(rs.getString(strCol2).toString());
					dbTotalCol2 += Double.parseDouble(rs.getString(strCol2)
							.toString());
				}
				if (rs.getString(strCol3) != null) {
					if (rs.getString(strCol3).toString().equals("0")
							|| rs.getString(strCol3).toString().equals("0.0"))
						obnv.setContentC3("");
					else
						obnv.setContentC3(rs.getString(strCol3).toString());
					dbTotalCol3 += Double.parseDouble(rs.getString(strCol3)
							.toString());
				}
				nuocvos.add(obnv);
			}
			obNuocVo obnv = new obNuocVo();
			obnv.setMaSP("Tổng");
			obnv.setContentC2(String.valueOf(dbTotalCol2));
			obnv.setContentC3(String.valueOf(dbTotalCol3));
			nuocvos.add(obnv);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return nuocvos;
	}

	private class NuocVoAdapter extends ArrayAdapter<obNuocVo> {

		private ArrayList<obNuocVo> NuocVos;

		public NuocVoAdapter(Context context, int textViewResourceId,
				ArrayList<obNuocVo> NuocVos) {
			super(context, textViewResourceId, NuocVos);
			this.NuocVos = new ArrayList<obNuocVo>();
			this.NuocVos.addAll(NuocVos);
		}

		private class ViewHolder {
			TextView tvMaSP;
			TextView tvContentC2;
			TextView tvContentC3;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder = null;

			if (convertView == null) {

				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = vi.inflate(R.layout.layout_tknuocvo, null);
				holder = new ViewHolder();
				holder.tvMaSP = (TextView) convertView
						.findViewById(R.id.tvMaSP);
				holder.tvContentC2 = (TextView) convertView
						.findViewById(R.id.tvContentC2);
				holder.tvContentC3 = (TextView) convertView
						.findViewById(R.id.tvContentC3);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			obNuocVo nuocvo = NuocVos.get(position);
			holder.tvMaSP.setText(nuocvo.getMaSP());
//			holder.tvMaSP.setTextSize(20);
			holder.tvContentC2.setText(Global.formatNumberG(nuocvo
					.getContentC2()));
//			holder.tvContentC2.setTextSize(20); 
			holder.tvContentC3.setText(Global.formatNumberG(nuocvo
					.getContentC3()));
//			holder.tvContentC3.setTextSize(20);
			return convertView;
		}

	}
}
